/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fx;

import beans.Beer;
import beans.Judge;
import dao.DaoJudge;
import fx.login.LoginController;
import fx.menu.MenuController;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Screen;
import javafx.stage.Stage;

/**
 *
 * @author Jandrei
 */
public class Home extends Application {

    private Stage stage;
    public static int LOGINWIDTH = 443;
    public static int LOGINHEIGTH = 197;
    public static int MENUWIDTH = 800;
    public static int MENUHEIGTH = 600;
    public static int JUDGEWIDTH = 455;
    public static int JUDGEHEIGTH = 310;
    public static int BEERWIDTH = 550;
    public static int BEERHEIGTH = 325;
    public static int BEERCHOOSERWIDTH = 572;
    public static int BEERCHOOSERHEIGTH = 310;
    public static int LEVELKNOWLEDGEWIDTH = 800;
    public static int LEVELKNOWLEDGEHEIGTH = 600;
    public static int BEERCHARACTERISTICSWIDTH = 1000;
    public static int BEERCHARACTERISTICSHEIGTH = 800;
    static GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    public static int AVALIATIONWIDTH = gd.getDisplayMode().getWidth() < 1107 ? gd.getDisplayMode().getWidth() : 1107;
    public static int AVALIATIONHEIGTH = 600;
    public static Judge loggedUser;
    public Beer beer;
    private DaoJudge dao;
//    public static Beer beerSelected;

    @Override
    public void start(Stage primaryStage) {
        try {
            stage = primaryStage;
            LoginController login = (LoginController) replaceSceneContent("login/Login.fxml", LOGINWIDTH, LOGINHEIGTH, "Perform the login");
            gotoLogin();
            primaryStage.show();
        } catch (Exception ex) {
            Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Application.launch(Home.class, (java.lang.String[]) null);
    }

    public Judge getLoggedUser() {
        return loggedUser;
    }

    public boolean userLogging(String userId, String password) {
        dao = new DaoJudge();
        Judge judge = dao.login(userId, password);
        if (judge != null) {
            loggedUser = judge;
            gotoMenu();
            return true;
        } else {
//            gotoLogin();
            return false;
        }
    }

    public Initializable replaceSceneContent(String fxml, int width, int heigth, String title) throws Exception {
        FXMLLoader loader = new FXMLLoader();
        InputStream in = Home.class.getResourceAsStream(fxml);
        loader.setBuilderFactory(new JavaFXBuilderFactory());
        loader.setLocation(Home.class.getResource(fxml));
        AnchorPane page;
        try {
            page = (AnchorPane) loader.load(in);
        } finally {
            in.close();
        }
        Scene scene = new Scene(page, width, heigth);
        stage.setScene(scene);
        stage.sizeToScene();
        stage.setResizable(false);
        stage.setTitle(title);
        return (Initializable) loader.getController();
    }

    public void gotoLogin() {
        try {
            LoginController login = (LoginController) replaceSceneContent("login/Login.fxml", LOGINWIDTH, LOGINHEIGTH, "Perform the login");
            login.setApp(this);
        } catch (Exception ex) {
            Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void gotoMenu() {
        try {
            MenuController menu = (MenuController) replaceSceneContent("menu/menu.fxml", MENUWIDTH, MENUHEIGTH, "BJCP-Scoresheet");
            menu.setMessage("info", "Welcome " + getLoggedUser().getName());
            menu.setApp(this);
        } catch (Exception ex) {
            Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
//    
//    public void setBeerSelected(Beer beer){
//        beerSelected = beer;
//    }

    public void setBeer(Beer beer) {
        this.beer = beer;
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
